package lzit.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBUtil {
    public static Connection getCon() {
        try {
            String drivername = "com.mysql.jdbc.Driver"; // 数据库驱动类名
            String conUrl = "jdbc:mysql://localhost:3306/yourDatabaseName"; // 数据库连接URL
            String username = "yourUsername"; // 数据库用户名
            String password = "yourPassword"; // 数据库密码
            Connection con = null;

            Class.forName(drivername);
            con = DriverManager.getConnection(conUrl, username, password);
            return con;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(ResultSet rs, Statement stmt, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            stmt = null;
        }

        if (con != null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            con = null;
        }
    }
}